﻿$(document).ready(function () {
    UpdateAccountControls();
});

function UpdateAccountControls() {
    var usage = "";
    if ($("#Usage").length > 0) {
        usage = "&usage=" + $("#Usage").val();
    }
    if($("#Site").length>0) {
        $("#Site").change(function () {
            if($("#UnitDiv").length>0) {
                var siteValue = $("#Site").val();
                
                SendRequest("/Account/GetUnitsBySiteId/?siteId=" + siteValue + usage, {}, "UnitDiv");
            }
            if ($("#BedDiv").length > 0) {
                $("#BedDiv").html("All available beds<input type=\"hidden\" id=\"Bed\" name=\"Bed\" value=\"All_Beds\"/>");
            }
        });
    }
    if ($("#Unit").length > 0) {
        $("#Unit").change(function () {
            if ($("#BedDiv").length > 0) {
                var siteValue = $("#Site").val();
                var unitValue = $("#Unit").val();
                SendRequest("/Account/GetUnitsBySiteId/?siteId=" + siteValue + "&unitId=" +unitValue + usage, {}, "BedDiv");
            }
        });
    }
    
    if($("#addPermission").length>0) {
        $("#addPermission").click(function() {
            if ($("#Site").length > 0 && $("#Unit").length > 0 && $("#Bed").length > 0 && $("#userId").length > 0) {
                var siteValue = $("#Site").val();
                var unitValue = $("#Unit").val();
                var bedValue = $("#Bed").val();
                var userId = $("#userId").val();
                SendRequest("/Account/SavePermission/?Site=" + siteValue + "&Unit=" + unitValue + "&Bed=" + bedValue + "&userId=" + userId, {}, "userPermissionDiv");
            }
        });
    }
    
    if ($("#changePermissions").length > 0) {
        $("#changePermissions").click(function () {
            var userId = $("#userId").val();
            SendRequest("/Account/UserPermissions/?userId=" + userId, {}, "userPermissionDiv");
        });
    }
    
    if ($(".delete-permission").length > 0) {
        $(".delete-permission").click(function() {
            var boardId = $(this).attr('id').replace("up", "");
            SendRequest("/Account/SavePermission/?action=delete&boardId=" + boardId , {}, "userPermissionDiv");
        });
    }
    
}
